﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessSMS;

namespace BussinessSMS
{

    public class Employees
    {
        private string UserId = string.Empty;
        private string SchoolId = string.Empty;
        public Employees(string userid,string schoolid)
        {
            this.UserId = userid;
            this.SchoolId = schoolid;
        }
        public List<DEGREE> GetDegrees()
        {
            DatabaseHelper _helper = new DatabaseHelper();
            using (var db = _helper.GetSMSData())
            {
                var degrees = (from x in db.DEGREEs where x.School_Id ==long.Parse(SchoolId) select x).ToList<DEGREE>();
                return degrees;
            }
        }
        public long GetMaxEmpId()
        {
            DatabaseHelper _helper = new DatabaseHelper();
            using (var db = _helper.GetSMSData())
            {
                
                var EmpId = (from x in db.Employees where x.School_Id == long.Parse(SchoolId) select  x);
                var maxid = EmpId.OrderByDescending(e => e.Emp_Id).SingleOrDefault();

                if (maxid!=null)
                    return maxid.Emp_Id + 1;
                else
                    return 101;
                
            }
        }
        public List<Employee> GetEmployeesList()
        {
            DatabaseHelper _helper = new DatabaseHelper();
            using (var db = _helper.GetSMSData())
            {
                var employees = (from x in db.Employees where x.School_Id == long.Parse(SchoolId) select x).ToList<Employee>();
                return employees;
            }
        }
        public void SaveEmpInfo(Employee EmpObj, List<Employee_Qualification> QualificationObj)
        {
            DatabaseHelper _helper = new DatabaseHelper();
            using (var db = _helper.GetSMSData())
            {
                var result = (from x in db.Employees where x.Emp_Id == EmpObj.Emp_Id && x.School_Id==long.Parse(SchoolId) select x).ToList<Employee>();
                if (result.Count() == 0)
                {                    
                    EmpObj.Created_By = UserId;
                    EmpObj.Created_Date = DateTime.Now;
                    EmpObj.School_Id = long.Parse(SchoolId);
                    db.GetTable<Employee>().InsertOnSubmit(EmpObj);
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (Exception e)
                    {
                        throw (e);
                    }

                }
                else
                {
                    try
                    {
                        foreach (Employee x in result)
                        {
                            x.Emp_Name = EmpObj.Emp_Name;
                            x.Blood_Group = EmpObj.Blood_Group;
                            x.Cell_No = EmpObj.Cell_No;
                            x.Date_of_Birth = EmpObj.Date_of_Birth;
                            x.Designation = EmpObj.Designation;
                            x.Email_Address = EmpObj.Email_Address;
                            x.Father_Name = EmpObj.Father_Name;
                            x.Gender = EmpObj.Gender;
                            x.Home_Address = EmpObj.Home_Address;
                            x.Home_Phone = EmpObj.Home_Phone;
                            x.Joining_Date = EmpObj.Joining_Date;
                            x.Leaving_Date = EmpObj.Leaving_Date;
                            x.Marital_Status = EmpObj.Marital_Status;
                            x.Modified_By = UserId;
                            x.Modified_Date = EmpObj.Modified_Date;
                            x.NIC = EmpObj.NIC;
                            x.Salary = EmpObj.Salary;
                            db.SubmitChanges();

                        }
                    }
                    catch (Exception e)
                    {
                        throw (e);
                    }
                }
                if (QualificationObj.Count() > 0)
                    SaveEmployeeQualification(QualificationObj);
            }
        }
        public void SaveEmployeeQualification(List<Employee_Qualification> QualificationObj)
        {
            DatabaseHelper _helper = new DatabaseHelper();
            using (var db = _helper.GetSMSData())
            {
                foreach (Employee_Qualification Qualification in QualificationObj)
                {
                    var result = (from x in db.Employee_Qualifications where x.Qualification_Id == Qualification.Qualification_Id && x.Emp_Id == Qualification.Emp_Id select x);
                    var count = result.Count();
                    if (count == 0)
                    {
                        try
                        {
                        Qualification.Qualification_Id = 121;
                        db.GetTable<Employee_Qualification>().InsertOnSubmit(Qualification);
                        db.SubmitChanges();
                        }
                        catch (Exception e)
                        {
                            throw (e);
                        }
                    }
                    else
                    {
                        try
                        {
                            foreach (Employee_Qualification x in result)
                            {
                                x.Division = Qualification.Division;
                                x.Passing_Year = Qualification.Passing_Year;
                                x.Institute = Qualification.Institute;                                
                                db.SubmitChanges();
                                
                            }
                        }
                        catch (Exception e)
                        {
                            throw (e);
                        }
                    }
                }
            }
        }
    }
}
